package com.ruoyi.yljf.service.impl;

import java.util.LinkedList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.yljf.domain.dto.CommonTypeDTO;
import com.ruoyi.yljf.domain.vo.CommonVO;
import com.ruoyi.yljf.domain.vo.CommonResV2VO;
import com.ruoyi.yljf.domain.vo.DrugResVO;
import com.ruoyi.yljf.domain.vo.ResCommonVO;
import com.ruoyi.yljf.util.CommonUtils;
import org.bouncycastle.util.Arrays;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.yljf.mapper.DrugMapper;
import com.ruoyi.yljf.domain.Drug;
import com.ruoyi.yljf.service.IDrugService;

import javax.annotation.Resource;

/**
 * 药材Service业务层处理
 * 
 * @author metaee
 * @date 2025-03-13
 */
@Service
public class DrugServiceImpl extends ServiceImpl<DrugMapper, Drug> implements IDrugService {
    @Resource
    private DrugMapper drugMapper;

	@Resource
	private CommonUtils utils;

    /**
     * 查询药材列表
     * 
     * @param drug 药材
     * @return 药材
     */
    @Override
    public List<Drug> selectDrugList(Drug drug) {
    	LambdaQueryWrapper<Drug> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotNull(drug.getId())) {
			queryWrapper.eq(Drug::getId, drug.getId());
		}
        if (StringUtils.isNotEmpty(drug.getName())) {
			queryWrapper.like(Drug::getName, drug.getName());
		}
        if (StringUtils.isNotNull(drug.getTasteId())) {
			queryWrapper.eq(Drug::getTasteId, drug.getTasteId());
		}
        if (StringUtils.isNotNull(drug.getMedicineId())) {
			queryWrapper.eq(Drug::getMedicineId, drug.getMedicineId());
		}
        if (StringUtils.isNotNull(drug.getEfficacyId())) {
			queryWrapper.eq(Drug::getEfficacyId, drug.getEfficacyId());
		}
        if (StringUtils.isNotNull(drug.getPinsId())) {
			queryWrapper.eq(Drug::getPinsId, drug.getPinsId());
		}
        if (StringUtils.isNotEmpty(drug.getDrugEfficacy())) {
			queryWrapper.eq(Drug::getDrugEfficacy, drug.getDrugEfficacy());
		}
        if (StringUtils.isNotEmpty(drug.getDosage())) {
			queryWrapper.eq(Drug::getDosage, drug.getDosage());
		}
        return drugMapper.selectList(queryWrapper);
    }

	@Override
	public ResCommonVO getDrugVO(CommonTypeDTO dto) {
		List<Drug> drugs = drugMapper.selectList(
				new LambdaQueryWrapper<Drug>()
						.like(StringUtils.isNotEmpty(dto.getName()),Drug::getName, dto.getName())
						.or().in(condition(dto.getCategoryIds()),Drug::getClassics,dto.getCategoryIds())
						.or().in(condition(dto.getCategoryIds()),Drug::getSupplement,dto.getCategoryIds())
						.or().in(condition(dto.getCategoryIds()),Drug::getPharmacopoeia,dto.getCategoryIds())
						.or().in(condition(dto.getCategoryIds()),Drug::getHuxinu,dto.getCategoryIds())
						.or().in(condition(dto.getCategoryIds()),Drug::getLiguanjie,dto.getCategoryIds())
						.or().in(condition(dto.getCategoryIds()),Drug::getOther,dto.getCategoryIds())
		);
		List<CommonVO> commonVOS = BeanUtil.copyToList(drugs, CommonVO.class);
		return new ResCommonVO(drugs.size(),utils.Listsort(commonVOS));
	}


	@Override
	public CommonResV2VO getDrugV2VO(CommonTypeDTO dto) {
		List<Drug> drugs = drugMapper.selectList(
				new LambdaQueryWrapper<Drug>()
						.like(StringUtils.isNotEmpty(dto.getName()),Drug::getName, dto.getName())
						.or().in(condition(dto.getCategoryIds()),Drug::getClassics,dto.getCategoryIds())
						.or().in(condition(dto.getCategoryIds()),Drug::getSupplement,dto.getCategoryIds())
						.or().in(condition(dto.getCategoryIds()),Drug::getPharmacopoeia,dto.getCategoryIds())
						.or().in(condition(dto.getCategoryIds()),Drug::getHuxinu,dto.getCategoryIds())
						.or().in(condition(dto.getCategoryIds()),Drug::getLiguanjie,dto.getCategoryIds())
						.or().in(condition(dto.getCategoryIds()),Drug::getOther,dto.getCategoryIds())
		);
		List<CommonVO> commonVOS = BeanUtil.copyToList(drugs, CommonVO.class);
		LinkedList<DrugResVO> drugResVOS = utils.ListsortV2(commonVOS);
		return new CommonResV2VO(drugs.size(),drugResVOS);
	}



	private boolean condition(Long[] arr){
		return !Arrays.isNullOrContainsNull(arr) && arr.length>0;
	}
}
